package com.example.demo1101.demo2;

public class SellTicket {

    public static void main(String[] args) {

        //多线程操作 多个线程操作同一个资源类
        Ticket ticket = new Ticket();

        //lambda表达式 (参数)->{代码}
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "A窗口").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();

            }
        }, "B窗口").start();

        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "C窗口").start();

        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        }, "D窗口").start();


    }
}

//资源类 OOP编程
class Ticket {
    //属性 方法
    private int number = 60;

    //买票的方式 synchronized本质就是排队 锁
    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (60 - (--number)) + "票，剩余" + number);
        }
    }
}